package com.infitecs.eyas.tool.service;

import com.infitecs.eyas.expression.service.ToolExpressionService;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by pzw on 2015/8/12.
 */
@Service("toolServices")
public class ToolService {

    @Autowired
    private ToolExpressionService toolExpressionService;

    @Autowired
    private SessionService sessionService;

    @Autowired
    private HttpSession httpSession;

    private Map<String,Double> paramMapTool = toolExpressionService.getParamMap();


    private Map<String,Double> mapResult = new HashMap<>();

    public void updateParam(Object o) {
        Param param = (Param) o;
        paramMapTool.put("PI", Math.PI);
        paramMapTool.put(sessionService.getE() + param.getParamName(), param.getParamValue());
        httpSession.setAttribute("toolMap", paramMapTool);
    }

    public Double airDoorCalE15() {
        Double E9 = 0.0;
        paramMapTool.put("TE9", E9);
        return toolExpressionService.calculate("TE15");
    }

    public Double calE15() {
        return toolExpressionService.calculate("TE15");
    }

    public Double transitionCalE15() {
        Map<String, Double> paramMap = toolExpressionService.getParamMap();
        Double E4 = paramMap.get("TE4");
        Double E4S= paramMap.get("TE4");
        Double E15;
        if (E4 == 3)
            E4 = 1.0;
        paramMap.put("TE4",E4);
        E15 = toolExpressionService.calculate("TE15");
        paramMap.put("TE4",E4S);
        return E15;
    }

    public void airDoorCalAll() {
        Map<String,Double> map = new HashMap<>();
        toolExpressionService.calculate("TE8");
        toolExpressionService.calculate("TE14");
        toolExpressionService.calculate("TE16");
        toolExpressionService.calculate("TE21");
        toolExpressionService.calculate("TE222");
        toolExpressionService.calculate("TE223");
        toolExpressionService.calculate("TE224");
        toolExpressionService.calculate("TE225");
        toolExpressionService.calculate("TE226");
        toolExpressionService.calculate("TE227");
        toolExpressionService.calculate("TE228");
        toolExpressionService.calculate("TE229");
        toolExpressionService.calculate("TE220");
        toolExpressionService.calculate("TE232");
        map.put("TE232", toolExpressionService.calculate("TE232"));
        map.put("e19", toolExpressionService.calculate("TE19"));
        map.put("e21",toolExpressionService.calculate("TE21"));
        map.put("e233",toolExpressionService.calculate("TE233"));
        map.put("e234",toolExpressionService.calculate("TE234"));
        mapResult = map;
    }

    public void bendCalAll() {
        Map<String,Double> map = new HashMap<>();
        Map<String, Double> paramMap = toolExpressionService.getParamMap();
        Double E17 = toolExpressionService.calculate("TE17");
        Double E32 = paramMap.get("TE32");
        Double isChecked = paramMap.get("isChecked");
        Double temp1 = E32 / E17;
        Double temp2 = E17 / E32;
        if (temp1 > 0.5) {
            toolExpressionService.calculate("TE34");
            toolExpressionService.calculate("TE35");
            toolExpressionService.calculate("TE36");
            toolExpressionService.calculate("TE37");
            toolExpressionService.calculate("TE38");
            toolExpressionService.calculate("TE39");
            map.put("e35",toolExpressionService.calculate("TE40"));
        } else if (temp1 <= 0.5 && isChecked == 0) {
            toolExpressionService.calculate("TE43");
            toolExpressionService.calculate("TE44");
            toolExpressionService.calculate("TE45");
            toolExpressionService.calculate("TE46");
            toolExpressionService.calculate("TE47");
            toolExpressionService.calculate("TE48");
            map.put("e35",toolExpressionService.calculate("TE49"));
        } else if (temp1 == 0 && isChecked == 0) {
            toolExpressionService.calculate("TE52");
            toolExpressionService.calculate("TE53");
            toolExpressionService.calculate("TE54");
            toolExpressionService.calculate("TE55");
            toolExpressionService.calculate("TE56");
            toolExpressionService.calculate("TE57");
            map.put("e35",toolExpressionService.calculate("TE58"));
        } else if (temp1 <= 0.5 && isChecked == 1) {
            toolExpressionService.calculate("TE62");
            toolExpressionService.calculate("TE63");
            toolExpressionService.calculate("TE64");
            toolExpressionService.calculate("TE65");
            toolExpressionService.calculate("TE66");
            toolExpressionService.calculate("TE67");
            map.put("e35",toolExpressionService.calculate("TE68"));
        } else if (temp1 == 0 && isChecked == 1) {
            toolExpressionService.calculate("TE71");
            toolExpressionService.calculate("TE72");
            toolExpressionService.calculate("TE73");
            toolExpressionService.calculate("TE74");
            toolExpressionService.calculate("TE74");
            map.put("e35",toolExpressionService.calculate("TE75"));
        }
        if (temp2 > 0.5) {
            map.put("e36",toolExpressionService.calculate("TE41"));
        } else if (temp2 <= 0.5 && isChecked == 0) {
            map.put("e36",toolExpressionService.calculate("TE50"));
        } else if (temp2 == 0 && isChecked == 0) {
            map.put("e36",toolExpressionService.calculate("TE59"));
        } else if (temp2 <= 0.5 && isChecked == 1) {
            map.put("e36",toolExpressionService.calculate("TE69"));
        } else if (temp2 == 0 && isChecked == 1) {
            map.put("e36",toolExpressionService.calculate("TE76"));
        }

        mapResult = map;
    }

    public void headerCalAll() {
        Map<String,Double> re = new HashMap<>();
        if (!toolExpressionService.hasParamValue("E15")) {
            re.put("E15", toolExpressionService.calculate("TE15"));
        }
        re.put("E19",toolExpressionService.calculate("TE19"));
        re.put("E21",toolExpressionService.calculate("TE21"));
        re.put("E240", toolExpressionService.calculate("TE240"));
        re.put("E248", toolExpressionService.calculate("TE248"));
        re.put("E249", toolExpressionService.calculate("TE249"));
        mapResult = re;
    }

    public void innerCalAll() {
        Map<String,Double> re = new HashMap<>();
        if (!toolExpressionService.hasParamValue("E15")) {
            re.put("E15", toolExpressionService.calculate("TE15"));
        }
        re.put("E19",toolExpressionService.calculate("TE19"));
        re.put("E21",toolExpressionService.calculate("TE21"));
        re.put("E210",toolExpressionService.calculate("TE210"));
        re.put("E211",toolExpressionService.calculate("TE211"));
        mapResult = re;
    }

    public void sinkholeCalAll() {
        Map<String,Double> map = new HashMap<>();
        toolExpressionService.calculate("TE14");
        map.put("e19", toolExpressionService.calculate("TE19"));
        map.put("e21",toolExpressionService.calculate("TE21"));
        map.put("e216",toolExpressionService.calculate("TE216"));
        map.put("e217",toolExpressionService.calculate("TE217"));
        mapResult = map;
    }

    public void strightCalAll() {
        Map<String, Double> map = new HashMap<>();
        toolExpressionService.calculate("TE14");
        toolExpressionService.calculate("TE16");
        map.put("e19", toolExpressionService.calculate("TE19"));
        map.put("e21", toolExpressionService.calculate("TE21"));
        toolExpressionService.calculate("TE23");
        map.put("e24", toolExpressionService.calculate("TE24"));
        map.put("e28", toolExpressionService.calculate("TE28"));
        map.put("e29", toolExpressionService.calculate("TE29"));
        mapResult = map;
    }


    public void transitionCalAll() {
        Map<String, Double> map = new HashMap<>();
        Map<String, Double> paramMap = toolExpressionService.getParamMap();
        Double E4 = paramMap.get("TE4");
        paramMap.put("TE4",E4);
        Double E93 = null;
        Double E9= paramMap.get("TE9");
        Double E98= E9;
        paramMap.put("TE98",E98);
        E93 = E4;
        if (E4 == 2)
            E93 = 2.0;
        if (E4 == 3)
            E93 = 2.0;
        if (E4 == 4)
            E93 = 1.0;
        paramMap.put("TE93",E93);
        toolExpressionService.calculate("TE14");
        if (E4==1 ){
            toolExpressionService.calculate("TE16");
            toolExpressionService.calculate("TE17");
            toolExpressionService.calculate("TE18");
            toolExpressionService.calculate("TE20");
            toolExpressionService.calculate("TE21");
            toolExpressionService.calculate("TE23");
            toolExpressionService.calculate("TE24");
            toolExpressionService.calculate("TE97");
            toolExpressionService.calculate("TE99");
            toolExpressionService.calculate("TE100");
            toolExpressionService.calculate("TE101");
            toolExpressionService.calculate("TE103");
            toolExpressionService.calculate("TE104");
            toolExpressionService.calculate("TE105");
            toolExpressionService.calculate("TE106");
            toolExpressionService.calculate("TE108");
            toolExpressionService.calculate("TE109");
            toolExpressionService.calculate("TE110");
            toolExpressionService.calculate("TE111");
            toolExpressionService.calculate("TE112");
            toolExpressionService.calculate("TE113");
            toolExpressionService.calculate("TE114");
            map.put("e21",toolExpressionService.calculate("TE21"));
            map.put("e19",toolExpressionService.calculate("TE19"));
            map.put("e106",toolExpressionService.calculate("TE106"));
            map.put("e1s",toolExpressionService.calculate("TE115"));
            map.put("e2s",toolExpressionService.calculate("TE108"));
            map.put("e3s",toolExpressionService.calculate("TE116"));
        }else if (E4 == 2){
            Double E6 = paramMap.get("TE6");
            Double E5 = paramMap.get("TE5");
            Double E94 = paramMap.get("TE94");
            Double E95 = paramMap.get("TE95");
            if (E6.equals(E95) || E5 .equals(E94)){
                toolExpressionService.calculate("TE8");
                toolExpressionService.calculate("TE16");
                toolExpressionService.calculate("TE17");
                toolExpressionService.calculate("TE19");
                toolExpressionService.calculate("TE20");
                toolExpressionService.calculate("TE21");
                toolExpressionService.calculate("TE22");
                toolExpressionService.calculate("TE23");
                toolExpressionService.calculate("TE24");
                toolExpressionService.calculate("TE100");
                toolExpressionService.calculate("TE101");
                toolExpressionService.calculate("TE103");
                toolExpressionService.calculate("TE104");
                toolExpressionService.calculate("TE105");
                toolExpressionService.calculate("TE118");
                toolExpressionService.calculate("TE119");
                toolExpressionService.calculate("TE130");
                toolExpressionService.calculate("TE131");
                toolExpressionService.calculate("TE132");
                toolExpressionService.calculate("TE133");
                toolExpressionService.calculate("TE134");
                toolExpressionService.calculate("TE135");
                toolExpressionService.calculate("TE136");
                toolExpressionService.calculate("TE137");
                toolExpressionService.calculate("TE138");
                toolExpressionService.calculate("TE139");
                map.put("e21",toolExpressionService.calculate("TE21"));
                map.put("e19",toolExpressionService.calculate("TE19"));
                map.put("e106",toolExpressionService.calculate("TE106"));
                map.put("e2s",toolExpressionService.calculate("TE132"));
                map.put("e1s",toolExpressionService.calculate("TE140"));
                map.put("e3s",toolExpressionService.calculate("TE141"));
            }else {
                toolExpressionService.calculate("TE8");
                Double E15 = paramMap.get("TE15");
                map.put("TE15",E15);
                toolExpressionService.calculate("TE16");
                toolExpressionService.calculate("TE17");
                toolExpressionService.calculate("TE19");
                toolExpressionService.calculate("TE20");
                toolExpressionService.calculate("TE21");
                toolExpressionService.calculate("TE22");
                toolExpressionService.calculate("TE23");
                toolExpressionService.calculate("TE24");
                toolExpressionService.calculate("TE100");
                toolExpressionService.calculate("TE101");
                toolExpressionService.calculate("TE103");
                toolExpressionService.calculate("TE104");
                toolExpressionService.calculate("TE105");
                toolExpressionService.calculate("TE118");
                toolExpressionService.calculate("TE119");
                toolExpressionService.calculate("TE120");
                toolExpressionService.calculate("TE121");
                toolExpressionService.calculate("TE122");
                toolExpressionService.calculate("TE123");
                toolExpressionService.calculate("TE124");
                toolExpressionService.calculate("TE125");
                toolExpressionService.calculate("TE126");
                map.put("e21",toolExpressionService.calculate("TE21"));
                map.put("e19",toolExpressionService.calculate("TE19"));
                map.put("e106",toolExpressionService.calculate("TE106"));
                map.put("e2s",toolExpressionService.calculate("TE120"));
                map.put("e1s",toolExpressionService.calculate("TE127"));
                map.put("e3s",toolExpressionService.calculate("TE128"));
            }
        }else if (E4==3 ){
            paramMap.put("TE4",1.0);
            toolExpressionService.calculate("TE8");
            toolExpressionService.calculate("TE16");
            toolExpressionService.calculate("TE17");
            toolExpressionService.calculate("TE18");
            toolExpressionService.calculate("TE20");
            toolExpressionService.calculate("TE21");
            toolExpressionService.calculate("TE23");
            toolExpressionService.calculate("TE24");
            toolExpressionService.calculate("TE100");
            toolExpressionService.calculate("TE101");
            toolExpressionService.calculate("TE103");
            toolExpressionService.calculate("TE104");
            toolExpressionService.calculate("TE105");
            toolExpressionService.calculate("TE143");
            toolExpressionService.calculate("TE144");
            toolExpressionService.calculate("TE17");
            toolExpressionService.calculate("TE145");
            toolExpressionService.calculate("TE146");
            toolExpressionService.calculate("TE147");
            toolExpressionService.calculate("TE148");
            toolExpressionService.calculate("TE149");
            map.put("e21",toolExpressionService.calculate("TE21"));
            map.put("e19",toolExpressionService.calculate("TE19"));
            map.put("e106",toolExpressionService.calculate("TE106"));
            map.put("e2s",toolExpressionService.calculate("TE143"));
            map.put("e1s",toolExpressionService.calculate("TE150"));
            map.put("e3s",toolExpressionService.calculate("TE151"));
        }else{
            paramMap.put("TE4",2.0);
            toolExpressionService.calculate("TE16");
            toolExpressionService.calculate("TE17");
            toolExpressionService.calculate("TE20");
            toolExpressionService.calculate("TE21");
            toolExpressionService.calculate("TE23");
            toolExpressionService.calculate("TE24");
            toolExpressionService.calculate("TE100");
            toolExpressionService.calculate("TE101");
            toolExpressionService.calculate("TE103");
            toolExpressionService.calculate("TE104");
            toolExpressionService.calculate("TE105");
            toolExpressionService.calculate("TE153");
            toolExpressionService.calculate("TE154");
            toolExpressionService.calculate("TE155");
            toolExpressionService.calculate("TE156");
            toolExpressionService.calculate("TE157");
            toolExpressionService.calculate("TE158");
            toolExpressionService.calculate("TE159");
            map.put("e21",toolExpressionService.calculate("TE21"));
            map.put("e19",toolExpressionService.calculate("TE19"));
            map.put("e106",toolExpressionService.calculate("TE106"));
            map.put("e2s",toolExpressionService.calculate("TE153"));
            map.put("e1s",toolExpressionService.calculate("TE160"));
            map.put("e3s",toolExpressionService.calculate("TE161"));
        }
        mapResult = map;
    }

    public void threeLinksCalAll(int type) {
        Map<String,Double> re = new HashMap<>();
        if (!toolExpressionService.hasParamValue("E15")) {
            re.put("E15", toolExpressionService.calculate("TE15"));
        }
        if (!toolExpressionService.hasParamValue("E172")) {
            re.put("E172", toolExpressionService.calculate("TE172"));
        }
        if (!toolExpressionService.hasParamValue("E181")) {
            re.put("E181", toolExpressionService.calculate("TE181"));
        }
        re.put("E19", toolExpressionService.calculate("TE19"));
        re.put("E21", toolExpressionService.calculate("TE21"));
        if (type == 1) {
            re.put("E185", toolExpressionService.calculate("TE185"));
            re.put("E186", toolExpressionService.calculate("TE186"));
            re.put("E187", toolExpressionService.calculate("TE187"));
            re.put("E188", toolExpressionService.calculate("TE188"));
            re.put("E189", toolExpressionService.calculate("TE189"));
            re.put("E190", toolExpressionService.calculate("TE190"));
        }else{
            re.put("E192", toolExpressionService.calculate("TE192"));
            re.put("E193", toolExpressionService.calculate("TE193"));
            re.put("E194", toolExpressionService.calculate("TE194"));
            re.put("E195", toolExpressionService.calculate("TE195"));
            re.put("E196", toolExpressionService.calculate("TE196"));
            re.put("E197", toolExpressionService.calculate("TE197"));
        }
        mapResult = re;
    }

    public Map<String, Double> getParamMapTool() {
        return paramMapTool;
    }

    public void setParamMapTool(Map<String, Double> paramMapTool) {
        this.paramMapTool = paramMapTool;
    }

    public Map<String, Double> getMapResult() {
        return mapResult;
    }

    public void setMapResult(Map<String, Double> mapResult) {
        this.mapResult = mapResult;
    }
}
